package com.example.shopping1902;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping1902.cart.PayActivity;
import com.example.shopping1902.goods.GoodsTypeBean;
import com.example.shopping1902.me.UserBean;
import com.example.shopping1902.utils.BaseActivity;
import com.example.shopping1902.utils.BuyDialog;
import com.example.shopping1902.utils.SaveUserUtils;
import com.example.shopping1902.utils.URLUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsInfoActivity extends BaseActivity implements View.OnClickListener {
    TextView nameTv,priceTv,desTv,numTv;
    Button buyBtn,cartBtn;
    ImageView backIv,bigIv;
    GridView likeGv;
    List<GoodsTypeBean.DataBean> data;
    private List<GoodsTypeBean.DataBean> data1;
    private LikeAdapter adapter;
    GoodsTypeBean.DataBean bean;
    private UserBean.DataBean userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_info);
        initView();
        userInfo = getUserInfo();

        //接受上一个页面传来的数据
        bean = (GoodsTypeBean.DataBean) getIntent().getSerializableExtra("info");
        showView(bean);
        connectNetWork(URLUtils.guessLikeURL);//联网
        setItemClick();

    }
    //猜你喜欢每一项的点击事件
    private void setItemClick() {
        likeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsTypeBean.DataBean bean = data.get(position);
                Intent intent = new Intent(GoodsInfoActivity.this, GoodsInfoActivity.class);
                intent.putExtra("info",bean);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onSuccess(String result) {
        GoodsTypeBean bean = new Gson().fromJson(result, GoodsTypeBean.class);
        if (bean.getData() == null) {
            if (bean.getCode()== 200){
                Toast.makeText(this,"添加购物车成功!",Toast.LENGTH_SHORT).show();
            }
        } else {
            List<GoodsTypeBean.DataBean> list = bean.getData();
            data.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }
    //*将数据显示在控件上*//
    private void showView(GoodsTypeBean.DataBean bean) {
        nameTv.setText(bean.getName());
        priceTv.setText("￥"+bean.getPrice()+"元");
        desTv.setText(bean.getInfo());
        numTv.setText("剩余:"+bean.getCount()+"件");
        String prcUrl = URLUtils.PUBLIC_URL+bean.getPhoto();
        Picasso.with(this).load(prcUrl).into(bigIv);
        //展示GridView的内容
        data = new ArrayList<>();
        //适配器
        adapter = new LikeAdapter(this, data);
        likeGv.setAdapter(adapter );
    }

    // 初始化控件
    private void initView() {
        nameTv = findViewById(R.id.info_tv_name);
        priceTv = findViewById(R.id.info_tv_price);
        desTv = findViewById(R.id.info_tv_des);
        numTv = findViewById(R.id.info_tv_num);
        buyBtn = findViewById(R.id.info_btn_buy);
        cartBtn = findViewById(R.id.info_btn_cart);
        backIv = findViewById(R.id.info_iv_back);
        bigIv = findViewById(R.id.info_iv_goods);
        likeGv = findViewById(R.id.info_gv_like);
        //设置点击事件
        buyBtn.setOnClickListener(this);
        cartBtn.setOnClickListener(this);
        backIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.info_btn_buy:
                if (userInfo == null) {
                    Toast.makeText(this,"购买之前请先登录！",Toast.LENGTH_SHORT).show();
                }else {
                    showBuyDialog(0);
                }
                break;
            case R.id.info_btn_cart:
                if (userInfo == null) {
                    Toast.makeText(this,"添加购物车之前请先登录！",Toast.LENGTH_SHORT).show();
                }else {
                    showBuyDialog(1);
                }
                break;
            case R.id.info_iv_back:
                finish();//销毁当前页面
                break;
        }
    }
    private UserBean.DataBean getUserInfo() {
        UserBean.DataBean bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean= SaveUserUtils.getUserToObj(this);
        }
        return bean;
    }

    //打开购买的对话框
    private void showBuyDialog(final int flag) {
        final BuyDialog dialog = new BuyDialog(this);
        dialog.show();
        dialog.setDialogWidth();
        dialog.setOnEnsureListener(new BuyDialog.OnEnsureListener() {
            @Override
            public void onEnsure(int count) {
                Intent intent = new Intent();
                if ((flag == 0)) {//跳转到支付界面
                    intent.setClass(GoodsInfoActivity.this, PayActivity.class);
                    intent.putExtra("count",count);//购买数量
                    intent.putExtra("info",bean);
                    startActivity(intent);
                }else if ( (flag == 1)){//跳转到购物车页面
                    //加入购物车
                    Map<String,String>map = new HashMap<>();
                    map.put("userId",userInfo.getId()+"");
                    map.put("commodityId",bean.getId()+"");
                    map.put("count",count+"");
                    postNetwork(URLUtils.addCartURL,map);
                    dialog.cancel();
                }
            }
        });
    }
}
